import random

import time
from django import http
from django.shortcuts import render
from django_redis import get_redis_connection

from libs.captcha.captcha import captcha  #设置为sources root，注意
# Create your views here.
from django.views import View
from libs.yuntongxun.sms import CCP



class ImaCodeView(View):
    def get(self,request,uuid):
        #1.通过captcha模块获取图片和图片内容
        text,image=captcha.generate_captcha()  #使用包里的生成
        #2.保存图片内容到redis
        # 保存图片验证码
        redis_conn = get_redis_connection('verify_code')
        redis_conn.setex('img_%s' % uuid, 300, text)


        #3.返回图片给前端
        return http.HttpResponse(image,content_type='image/jpg')

class SMSCodeView(View):
    def get(self,request,mobile):
        #1.初始化数据 mobile,image_code,uuid
        image_code=request.GET.get('image_code').upper()  #大小写变换
        image_code_id=request.GET.get('image_code_id')
        #1.2校验数据
        if not all([image_code,image_code_id]):
            return http.HttpResponseForbidden('参数不全')
        redis_conn = get_redis_connection('verify_code')
        redis_image_code=redis_conn.get('img_%s' %image_code_id).decode()
        if image_code != redis_image_code:
            return http.HttpResponseForbidden('图片验证码不正确')

        # 1,初始化数据 mobile, image_code, uuid
        # image_code = request.GET.get('image_code').upper()
        # image_code_id = request.GET.get('image_code_id')
        # # # 1,2 校验数据
        # if not all([image_code, image_code_id]):
        #     return http.JsonResponse({'errmsg': '参数不全'})
        # redis_conn = get_redis_connection('verify_code')
        # redis_image_code = redis_conn.get('img_%s' % image_code_id).decode()
        # if image_code != redis_image_code:
        #     return http.JsonResponse({'errmsg': '图片验证码不正确'})

        redis_sms_flag = redis_conn.get('sms_flag_%s' % mobile)
        if redis_sms_flag:
            return http.HttpResponseForbidden('不能频繁请求短信')
        # # 2.处理业务逻辑
        # #补充是否是再发送中
        # #获取随机短信验证码
        # sms_code ="%04d" %random.randint(0,9999)
        # #保存验证码到redis中
        # pl=redis_conn.pipeline()     #使用pipeline执行
        # # redis_conn.setex('sms_%s'%mobile,300,sms_code)
        # # redis_conn.setex('sms_flag_%s' %mobile,60,'sending')
        # pl.redis_conn.setex('sms_%s'%mobile,300,sms_code)
        # pl.redis_conn.setex('sms_flag_%s' %mobile,60,'sending')
        # pl.execute()

        # 2,处理业务逻辑
        # 获取随机短信验证码
        sms_code = '%04d' % random.randint(0, 9999)
        # 保持验证码到redis中
        pl = redis_conn.pipeline()
        # redis_conn.setex('sms_%s' % mobile, 300, sms_code)
        # redis_conn.setex('sms_flag_%s' % mobile, 60, 'sending')
        pl.setex('sms_%s' % mobile, 300, sms_code)
        pl.setex('sms_flag_%s' % mobile, 60, 'sending')
        pl.execute()

        #发送短信验证码
        # ccp=CCP()  #迁移到其他的地方了
        # ccp.send_template_sms(mobile,[sms_code,5],1)

        ######异步调用
        # time.sleep(5)
        # send_sms.delay(mobile,sms_code,5,1)
        print('sms_code:%s' % sms_code)

        #3.返回响应
        res={
            'code':0,
            'errmsg':'success'
        }
        return http.JsonResponse(res)
        pass

















